home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 August
/
Macworld (1999-08).dmg
/
Shareware World
/
Info
/
For Developers
/
MADE 1.4.0
/
Essentials
/
Essential Events.c
< prev
next >
Wrap
Text File
|
1999-05-26
|
11KB
|
437 lines
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* MADE - Macintosh Application Development Essentials */
/* --------------------------------------------------- */
/* (c) Sig Software, http://www.sigsoftware.com/ */
/* */
/* These files can only be used for experimental purposes. To obtain */
/* fully commented code, source code for the functions in Essential */
/* Extras.h and permission for usage in final projects, you must */
/* purchase a license. See documentation for more information. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* Essential Events.c */
/* ------------------ */
/* */
/* Event and AppleEvent handling. */
/* */
/* Version 1.0.0 - 10th November 1996 */
/* Version 1.1.0 - 29th January 1998 - Modeless dialog support */
/* Version 1.2.0 - 20th November 1998 - Avoids C/C++ warnings */
/* Open App event support */
/* Suspend/resume events */
/* Version 1.4.0 - 26th May 1999 - Rearranged, threads, appInFront */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "Essential Headers.h"
#include "Essential Prototypes.h"
#include "Essential Extras.h"
WindowPtr frontWindow=0;
Boolean appInFront=true;
#if Use_AppleEvents
#include <AppleEvents.h>
#if Handle_Open_Application_Event
pascal OSErr EventOpenAppHandler(AppleEvent*, AppleEvent*, long);
AEEventHandlerUPP EventOpenAppUPP;
#endif
#if Handle_Open_Documents_Event
pascal OSErr EventOpenDocHandler(AppleEvent*, AppleEvent*, long);
AEEventHandlerUPP EventOpenDocUPP;
#endif
#if Handle_Print_Documents_Event
pascal OSErr EventPrintDocHandler(AppleEvent*, AppleEvent*, long);
AEEventHandlerUPP EventPrintDocUPP;
#endif
#if Handle_Quit_Application_Event
pascal OSErr EventQuitAppHandler(AppleEvent*, AppleEvent*, long);
AEEventHandlerUPP EventQuitAppUPP;
#endif
#endif
void MainEventLoop()
{
EventRecord theEvent;
Point localPoint;
#if Add_Threads_Support
WaitNextEvent(everyEvent, &theEvent,
ThreadsBusy() ? Event_Sleep_Time_If_Threads_Busy : Event_Sleep_Time, 0);
#else
WaitNextEvent(everyEvent, &theEvent, Event_Sleep_Time, 0);
#endif
if (frontWindow) {
localPoint=theEvent.where;
GlobalToLocal(&localPoint);
MySetTheCursor(localPoint);
} else
SetCursor(&qd.arrow);
ProcessEvent(&theEvent);
}
void ProcessEvent(EventRecord* theEvent)
{
char keyCharacter;
short mousePart;
WindowPtr mouseWindow, oldPort;
DialogPtr theDialog;
Rect growLimits;
long growResult;
Point localPoint;
Error error=0;
mousePart=FindWindow(theEvent->where, &mouseWindow);
if (mousePart==inContent) {
localPoint=theEvent->where;
GetPort(&oldPort);
SetPort(mouseWindow);
GlobalToLocal(&localPoint);
SetPort(oldPort);
}
#if Support_Modeless_Dialogs
if (IsDialogEvent(theEvent)) {
switch (theEvent->what) {
case mouseDown:
theDialog=mouseWindow;
break;
case activateEvt:
if (theEvent->modifiers&activeFlag) {
frontWindow=(WindowPtr)theEvent->message;
SetPort(frontWindow);
} else
frontWindow=0;
case updateEvt:
theDialog=(WindowPtr)theEvent->message;
break;
case nullEvent:
MyPerformIdleTasks();
return;
default:
theDialog=FrontWindow();
break;
}
if (MyDialogFilter(theDialog, theEvent, &mousePart) ||
DialogSelect(theEvent, &theDialog, &mousePart))
MyHandleDialogEvent(theEvent, theDialog, mousePart);
return;
}
#endif
switch (theEvent->what) {
case nullEvent:
MyPerformIdleTasks();
break;
case keyDown: case autoKey: // keyDown and autoKey not distinguished between
keyCharacter=(char)(theEvent->message&charCodeMask);
if (theEvent->modifiers&cmdKey) {
MyEnableMenus();
SelectMenuItem(MenuKey(keyCharacter));
} else
MyHandleKeyDown(keyCharacter, theEvent);
break;
case mouseDown:
switch (mousePart) {
case inMenuBar:
MyEnableMenus();
SelectMenuItem(MenuSelect(theEvent->where));
break;
case inSysWindow:
SystemClick(theEvent, mouseWindow);
break;
case inContent:
if (mouseWindow==frontWindow)
MyHandleMouseDown(localPoint, theEvent);
else
SelectWindow(mouseWindow);
break;
case inDrag:
DragWindow(mouseWindow, theEvent->where, &qd.screenBits.bounds);
break;
case inGrow:
if (mouseWindow==frontWindow) {
MyResizeBounds(&growLimits);
growResult=GrowWindow(frontWindow, theEvent->where, &growLimits);
if (growResult) {
SizeWindow(frontWindow, LoWord(growResult), HiWord(growResult), true);
// last parameter specifies that new bit should be updated
MySizedWindow();
}
} else
SelectWindow(mouseWindow);
break;
case inGoAway:
if (mouseWindow==frontWindow) {
if (TrackGoAway(frontWindow, theEvent->where)) {
MyCloseWindow();
frontWindow=0;
}
} else
SelectWindow(mouseWindow);
break;
case inZoomIn: case inZoomOut:
if (mouseWindow==frontWindow) {
if (TrackBox(frontWindow, theEvent->where, mousePart)) {
ZoomWindow(frontWindow, mousePart, false);
// All sorts of things could be done here but this is default behaviour
MySizedWindow();
}
} else
SelectWindow(mouseWindow);
break;
}
break;
case updateEvt:
BeginUpdate((WindowPtr)theEvent->message);
GetPort(&oldPort);
SetPort((WindowPtr)theEvent->message);
MyDrawWindow((WindowPtr)theEvent->message);
SetPort(oldPort);
EndUpdate((WindowPtr)theEvent->message);
break;
case activateEvt:
if (theEvent->modifiers&activeFlag) {
frontWindow=(WindowPtr)theEvent->message;
SetPort(frontWindow);
// This is essential - all window-based actions depend on this call
MyActivateWindow();
} else {
MyDeactivateWindow();
frontWindow=0;
}
break;
case osEvt:
if ((theEvent->message&osEvtMessageMask)>>24==suspendResumeMessage) {
if (theEvent->message&1) {
appInFront=true;
MyResumeApplication();
} else {
appInFront=false;
MySuspendApplication();
}
}
break;
#if Use_AppleEvents
case kHighLevelEvent:
error=AEProcessAppleEvent(theEvent);
if (error!=errAEEventNotHandled)
TestError(error);
break;
#endif
}
}
#if Use_AppleEvents
Error CheckRequiredParams(AppleEvent *appleEvent)
{
Error error=0;
DescType returnType;
Size actualSize;
error=AEGetAttributePtr(appleEvent, keyMissedKeywordAttr,
typeWildCard, &returnType, 0, 0, &actualSize);
if (error==errAEDescNotFound)
error=0;
else if (error==0)
error=errAEEventNotHandled;
return error;
}
Error InitAppleEvents()
{
Error error=0;
// NOTE: the refCon parameter in the AEInstallEventHandler call need not be zero; you can
// pass a long word in that will get given to your handlers when they are called
#if Handle_Open_Application_Event
EventOpenAppUPP=NewAEEventHandlerProc(EventOpenAppHandler);
error=AEInstallEventHandler(
kCoreEventClass, kAEOpenApplication, EventOpenAppUPP, 0, false);
TestError(error);
_i(error)
#endif
#if Handle_Open_Documents_Event
EventOpenDocUPP=NewAEEventHandlerProc(EventOpenDocHandler);
error=AEInstallEventHandler(
kCoreEventClass, kAEOpenDocuments, EventOpenDocUPP, 0, false);
TestError(error);
_i(error)
#endif
#if Handle_Print_Documents_Event
EventPrintDocUPP=NewAEEventHandlerProc(EventPrintDocHandler);
error=AEInstallEventHandler(
kCoreEventClass, kAEPrintDocuments, EventPrintDocUPP, 0, false);
TestError(error);
_i(error)
#endif
#if Handle_Quit_Application_Event
EventQuitAppUPP=NewAEEventHandlerProc(EventQuitAppHandler);
error=AEInstallEventHandler(
kCoreEventClass, kAEQuitApplication, EventQuitAppUPP, 0, false);
TestError(error);
#endif
_e
return error;
}
#if Handle_Open_Application_Event
pascal OSErr EventOpenAppHandler(AppleEvent* eventIn, AppleEvent* eventOut, long refCon)
{
Error error;
error=CheckRequiredParams(eventIn);
_i(error)
MyOpenApplication();
_e
return error;
}
#endif
#if Handle_Open_Documents_Event
pascal OSErr EventOpenDocHandler(AppleEvent* eventIn, AppleEvent* eventOut, long refCon)
{
AEDescList docList;
DescType returnType;
Size actualSize;
long docNum, numDocs;
FSSpec fileSpec;
AEKeyword keyword;
Error error=0;
error=AEGetParamDesc(eventIn, keyDirectObject, typeAEList, &docList);
_i(error)
error=CheckRequiredParams(eventIn);
_i(error)
error=AECountItems(&docList, &numDocs);
_i(error)
for (docNum=1; docNum<=numDocs; docNum++) {
error=AEGetNthPtr(&docList, docNum, typeFSS, &keyword,
&returnType, &fileSpec, sizeof(fileSpec), &actualSize);
_i(error)
MyOpenDocument(&fileSpec);
}
error=AEDisposeDesc(&docList);
_e
return error;
}
#endif
#if Handle_Print_Documents_Event
pascal OSErr EventPrintDocHandler(AppleEvent* eventIn, AppleEvent* eventOut, long refCon)
{
AEDescList docList;
DescType returnType;
Size actualSize;
long docNum, numDocs;
FSSpec fileSpec;
AEKeyword keyword;
Error error=0;
error=AEGetParamDesc(eventIn, keyDirectObject, typeAEList, &docList);
_i(error)
error=CheckRequiredParams(eventIn);
_i(error)
error=AECountItems(&docList, &numDocs);
_i(error)
for (docNum=1; docNum<=numDocs; docNum++) {
error=AEGetNthPtr(&docList, docNum, typeFSS, &keyword,
&returnType, &fileSpec, sizeof(fileSpec), &actualSize);
_i(error)
MyPrintDocument(&fileSpec);
}
error=AEDisposeDesc(&docList);
_e
return error;
}
#endif
#if Handle_Quit_Application_Event
pascal OSErr EventQuitAppHandler(AppleEvent* event, AppleEvent*, long)
{
Error error=0;
error=CheckRequiredParams(event);
_i(error);
applicationHasQuit=true;
_e
return error;
}
#endif
#endif